package com.example.pingze.service;

import com.example.pingze.entity.PoetInfo;
import com.example.pingze.exception.PingZeException;
import com.example.pingze.repository.PoetInfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class PoetInfoService {
    @Autowired
    private PoetInfoRepository repository;

    //不正常使用（有逻辑错误，诗人应该返回诗人列表）
    public PoetInfo findPoetInfoByPoetName(String name) {
        return repository.findPoetInfoByPoetName(name);
    }

    public List<PoetInfo> findPoetByName(String name) {
        return repository.findPoetInfoByPoetNameContains(name);
    }

    public Page getPoetInfoList(PoetInfo poet, Pageable pageable) {
        Specification<PoetInfo> specification=new Specification<PoetInfo>() {
            @Override
            public Predicate toPredicate(Root<PoetInfo> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(!StringUtils.isEmpty(poet.getId())){
                    predicates.add(criteriaBuilder.equal(root.get("id"),poet.getId()));
                }
                if(!StringUtils.isEmpty(poet.getPoetName())){
                    predicates.add(criteriaBuilder.equal(root.get("name"),poet.getPoetName()));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        Page page = repository.findAll(specification,pageable);
        return page;
    }
    public PoetInfo add(String name,String headImageUrl,String simpleIntro){
        if(StringUtils.isEmpty(name)){
            throw new PingZeException("名字不能为空");
        }
        if(StringUtils.isEmpty(headImageUrl)){
            throw new PingZeException("头像不能为空");
        }
        if(StringUtils.isEmpty(simpleIntro)){
            throw new PingZeException("简介不能为空");
        }
        PoetInfo add=new PoetInfo();
        add.setId(UUID.randomUUID().toString());
        add.setPoetName(name);
        add.setHeadImageUrl(headImageUrl);
        add.setSimpleIntro(simpleIntro);
        PoetInfo save = repository.save(add);
        return save;
    }
    public PoetInfo edit(String id,String name,String headImageUrl,String simpleIntro) {
        PoetInfo update = repository.findById(id).get();
        if(!update.getPoetName().equals(name)){
            update.setPoetName(name);
        }
        if(!update.getHeadImageUrl().equals(headImageUrl)){
            update.setHeadImageUrl(headImageUrl);
        }
        if(!update.getSimpleIntro().equals(simpleIntro)){
            update.setSimpleIntro(simpleIntro);
        }
        repository.save(update);
        return update;
    }

    public PoetInfo findPoetInfoById(String id) {
        return repository.findPoetInfoById(id);
    }

    public void delete(PoetInfo poetInfo) {
        repository.delete(poetInfo);
    }
}
